// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _modNum ("条纹数量", Range(2,30)) = 10.0
        _uRatio ("占比", Range(0,1)) = 0.3

    }
    SubShader
    {
        Tags { "RenderType"="transparent"}
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0
        #define MAX_MARCHING_STEPS 100
#define MIN_DIST 0.01
#define MAX_DIST 100.0
#define EPSILON 0.001
        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        half _modNum;
        half _uRatio;
        fixed4 _Color;
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
         // 球体sdf
        float sdSphere(float3 p,float r){
            return length(p)-r;
        }
  // 平面sdf
        float sdPlane(float3 p,float3 n,float h){
            n = normalize(n);
            return dot(p,n) + h;
        }

// 最后场景
        float sdScene(float3 p){
            // 定义球体
            float3 spherePos = float3(0.0,1.0,0.0);
            float sphereRadius =1.0;
            float3 sphereNormal = normalize( p - spherePos);
            // 定义平面
            float3 planePos = float3(0.0,0.0,0.0);
            float3 planeNormal = float3(0.0,1.0,0.0);
            // 求交集
            float sphereDist = sdSphere(p-spherePos,sphereRadius);
            float planeDist = sdPlane(p-planePos,planeNormal,0.0);
            float dist = min(sphereDist,planeDist);
            return dist;
        }

// RayMarch，光线和物体交点
float RayMarch(float3 ro,float3 rd){
    float depth = 0.0;
    for(int i=0;i<MAX_MARCHING_STEPS;i++){
        float3 pos = ro + rd*depth;
        float dist = sdScene(pos);
        depth += dist;

        if(depth > MAX_DIST || depth < MIN_DIST ){
            break;
        }
        return depth;
    }
    }

// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================
// =========================================================

        float sdCircle(float2 p,float r){
            return length(p)-r;
        }
        // https://iquilezles.org/articles/distfunctions2d/
        float sdBox(float2 p, float2 b )
        {
            float2 d = abs(p)-b;
            return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
        }
        float sdStar5(float2 p, float r, float rf)
        {
            float2 k1 = fixed2(0.809016994375, -0.587785252292);
            float2 k2 = fixed2(-k1.x,k1.y);
            p.x = abs(p.x);
            p -= 2.0*max(dot(k1,p),0.0)*k1;
            p -= 2.0*max(dot(k2,p),0.0)*k2;
            p.x = abs(p.x);
            p.y -= r;
            float2 ba = rf*fixed2(-k1.y,k1.x) - fixed2(0,1);
            float h = clamp( dot(p,ba)/dot(ba,ba), 0.0, r );
            return length(p-ba*h) * sign(p.y*ba.x-p.x*ba.y);
        }

        float sdHexagon(float2 p,float r){
            float3 k = fixed3(-0.86,0.5,0.57);
            p = abs(p);
            p-=2.0*min(dot(k.xy,p),0.0)*k.xy;
            p-=fixed2(clamp(p.x,-k.z*r,k.z*r),r);
            return length(p)*sign(p.y);
        }
        // 倒圆角 
        float opRound(float sdf,float r){
            return sdf-r;
        }
// 圆环
        float opAnnular(float sdf,float r){
            return abs(sdf)-r;
        }
        float3x3 move2d(float2 p){
            return float3x3(
                1,0,-p.x,
                0,1,-p.y,
                0,0,1
            );
        }
        float3x3 rotate2d(float r){
            float c = cos(r);
            float s = sin(r);
            return float3x3(
                c,-s,0,
                s,c,0,
                0,0,1
            );
        }
        float3x3 scale2d(float2 s){
            return float3x3(
             1/s.x,0,0,
             0,1/s.y,0,
             0,0,1
            );
        }
        // 伪随机函数
        float random(float2 uv){
            return frac(sin(dot(uv.xy,float2(12.9898,78.233))) * 43758.5453);
        }
    //   分形
        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

       

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
              fixed4 finalColor = RayMarch(IN.worldPos, normalize(IN.viewDir));
                o.Albedo = finalColor.rgb;
                o.Alpha = finalColor.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
